package com.hilin.hilinim.common;

import com.hilin.common.util.JwtUtils;
import com.hilin.entity.po.AccountInfoPo;
import com.hilin.etcd.client.HilinEtcdClient;
import com.hilin.hilinim.netty.ws.IWsHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.multipart.Attribute;
import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder;
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ImWsHandler implements IWsHandler {
    /**
     * token参数名称
     */
    public static final String PK_TOKEN = "token";

    public static final String RK_USER_STATUS = "im:user:status:%s:%s";

    @Autowired
    private HilinEtcdClient hilinEtcdClient;

    @Override
    public AccountInfoPo handshake(FullHttpRequest request, Channel channel) {
        try {
            String token = parse(request).get(PK_TOKEN);
            return JwtUtils.check(token, AccountInfoPo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void afterHandshake(AccountInfoPo user, Channel channel) {
    }

    @Override
    public String onMsg(AccountInfoPo user, String msg, Channel channel) {
        return null;
    }

    @Override
    public ByteBuf onMsg(AccountInfoPo user, ByteBuf msg, Channel channel) {
        return null;
    }

    @Override
    public void onMsg(AccountInfoPo user, Channel channel) {
//        hilinEtcdClient.opsForValue().set(String.format(RK_USER_STATUS, user.getOrgId(), user.getAccountId()), "1", 300, TimeUnit.SECONDS);
    }

    @Override
    public void close(AccountInfoPo user, String reason, Channel channel) {
    }

    /**
     * 解析请求参数
     *
     * @return 包含所有请求参数的键值对, 如果没有参数, 则返回空Map
     */
    public static Map<String, String> parse(FullHttpRequest fullReq) throws Exception {
        HttpMethod method = fullReq.method();

        Map<String, String> parmMap = new HashMap<>();

        if (HttpMethod.GET == method) {
            // 是GET请求
            QueryStringDecoder decoder = new QueryStringDecoder(fullReq.uri());
            decoder.parameters().forEach((key, value) -> {
                // entry.getValue()是一个List, 只取第一个元素
                parmMap.put(key, value.get(0));
            });
        } else if (HttpMethod.POST == method) {
            // 是POST请求
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(fullReq);
            decoder.offer(fullReq);
            List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas();
            for (InterfaceHttpData parm : parmList) {
                Attribute data = (Attribute) parm;
                parmMap.put(data.getName(), data.getValue());
            }

        }
        return parmMap;
    }
}
